﻿#region License
/* **********************************************************************************
 * Copyright (c) Roman Ivantsov
 * This source code is subject to terms and conditions of the MIT License
 * for Irony. A copy of the license can be found in the License.txt file
 * at the root of this distribution. 
 * By using this source code in any fashion, you are agreeing to be bound by the terms of the 
 * MIT License.
 * You must not remove this notice from this software.
 * **********************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UserScript.ExpressionParsing.Irony.Parsing {

  internal struct SourceLocation {
    internal int Position;
    /// <summary>Source line number, 0-based.</summary>
    internal int Line;
    /// <summary>Source column number, 0-based.</summary>
    internal int Column;
    internal SourceLocation(int position, int line, int column) {
      Position = position;
      Line = line;
      Column = column;
    }
    //Line/col are zero-based internally
    public override string ToString() {
      return string.Format(Resources.FmtRowCol, Line + 1, Column + 1);
    }
    //Line and Column displayed to user should be 1-based
    internal string ToUiString() {
      return string.Format(Resources.FmtRowCol, Line + 1, Column + 1);
    }
    internal static int Compare(SourceLocation x, SourceLocation y) {
      if (x.Position < y.Position) return -1;
      if (x.Position == y.Position) return 0;
      return 1;
    }
    internal static SourceLocation Empty {
      get { return _empty; }
    } static SourceLocation _empty = new SourceLocation();

    public static SourceLocation operator +(SourceLocation x, SourceLocation y)
    {
      return new SourceLocation(x.Position + y.Position, x.Line + y.Line, x.Column + y.Column); 
    }
    public static SourceLocation operator +(SourceLocation x, int offset)
    {
      return new SourceLocation(x.Position + offset, x.Line, x.Column + offset); 
    }
  }//SourceLocation

  internal struct SourceSpan {
    internal readonly SourceLocation Location;
    internal readonly int Length;
    internal SourceSpan(SourceLocation location, int length) {
      Location = location;
      Length = length;
    }
    internal int EndPosition {
      get { return Location.Position + Length; }
    }
    internal bool InRange(int position) {
      return (position >= Location.Position && position <= EndPosition);
    }

  }


}//namespace
